/* 
 * File:   Line.cpp
 * Author: Guilherme Monteiro
 */

#ifndef LINE_H
#define	LINE_H

#include <cv.h>
#include <highgui.h>
#include <cxcore.h>

using namespace std;

/**
 * Struct that represents a line.
 */
struct Line {
    static int const FLAG_TO_PARALLEL_LINES = -1;
    
    CvPoint p1;
    CvPoint p2;
    
    /**
     * Constructor 1 to Line struct.
     */
    Line() {
        p1 = cvPoint(-1,-1);
        p2 = cvPoint(-1,-1);
    }
    
    /**
     * Constructor2 to Line struct.
     * 
     * @param _p1
     * @param _p2
     */
    Line(CvPoint _p1, CvPoint _p2) {
        p1 = _p1;
        p2 = _p2;
    }
    
    /**
     * Returns if this line
     * is equal to another.
     * 
     * @param otherLine
     * @return true is equal
     */
    bool equals(Line otherLine) {
        return p1.x == otherLine.p1.x && p1.y == otherLine.p1.y &&
                p2.x == otherLine.p2.x && p2.y == otherLine.p2.y;
    }
    
    /**
     * Returns if this line is horizontal.
     * 
     * @return true if is horizontal
     */
    bool isHorizontal() {
        return p1.y == p2.y && p1.x != p2.x;
    }
    
    /**
     * Returns if this line is vertical.
     * 
     * @return true if is vertical
     */
    bool isVertical() {
        return p1.y != p2.y && p1.x == p2.x;
    }
    
    /**
     * Returns the intersection point
     * between this line and another.
     * 
     * note: are vertical or horizontal lines only
     * 
     * @param otherLine
     * @return intersection point
     */
    CvPoint intersection(Line otherLine) {
        if(this->isHorizontal() && otherLine.isVertical()) {
            return cvPoint(otherLine.p1.x,this->p1.y);
        }
        else if(this->isVertical() && otherLine.isHorizontal()) {
            return cvPoint(this->p1.x, otherLine.p1.y);
        }
        else {
            return cvPoint(FLAG_TO_PARALLEL_LINES, FLAG_TO_PARALLEL_LINES);
        }
    }
};

#endif	/* LINE_H */
